/**
 * File:   DHTFrame.java
 * Author: Gvennet
 */
package gui.main;

import dht.network.Network;
import dht.node.DHTDataModel;
import dht.node.DHTNodeModel;
import dht.node.DHTStatistics;
import gui.actions.ExitAction;
import gui.tabs.LocalDataTab;
import gui.tabs.NodesTab;
import gui.tabs.PutGetDataTab;
import gui.tabs.StatisticsTab;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * todo write javadoc
 */
public final class DHTFrame
{

	private DHTFrame()
	{
	}

	public static JFrame createFrame(String title,
									 DHTNodeModel nodeModel,
									 DHTDataModel dataModel,
									 DHTDataTableModel localDataTableModel,
									 DHTDataTableModel getDataTableModel,
									 Network network,
									 ScheduledThreadPoolExecutor scheduledExecutor,
									 DHTStatistics statistics)
	{
		JFrame frame = new JFrame(title);


		final Action exitAction = new ExitAction(frame, scheduledExecutor, network);

		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				exitAction.actionPerformed(null);
			}

		});


		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu applicationMenu = menuBar.add(new JMenu("Файл"));
		applicationMenu.setMnemonic('F');
		applicationMenu.addSeparator();
		applicationMenu.add(new JMenuItem(exitAction));

		JTabbedPane tabbedPane = new JTabbedPane();

		tabbedPane.add("Таблица маршрутизации", NodesTab.createNodesTab(frame, network, nodeModel));
		tabbedPane.add("Локальные данные", LocalDataTab.createLocalDataTab(localDataTableModel));
		tabbedPane.add("Ввод/поиск данных", PutGetDataTab.createPutGetDataTab(getDataTableModel, nodeModel, dataModel, network));
		if (statistics != null)
		{
			StatisticsTab statisticsTab = new StatisticsTab(statistics, frame);
			statistics.addNetworkListener(statisticsTab);
			tabbedPane.add("Статистика", statisticsTab.getComponent());
		}

		frame.add(tabbedPane);

		frame.getRootPane().setPreferredSize(new Dimension(720, 480));

		frame.pack();

		frame.setLocationByPlatform(true);
		frame.setVisible(true);


		return frame;
	}
}
